package com.gjl.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MqConfig {
    //config配置类上添加注解@Configuration

//    @Bean(name="Bmessage")
//    public Queue BMessage(){
//        return new Queue("Bmessage");
//    }//声明队列

    @Bean(name="word")
    public Queue word(){
        return new Queue("word");
    }//声明队列

//    @Bean
//    FanoutExchange fanoutExchange(){
//        return new FanoutExchange("fanoutExchange");
//    }//声明交换机

    @Bean
    FanoutExchange fasonghelloword(){
        return new FanoutExchange("fasonghelloword");
    }//声明交换机

//    @Bean
//    Binding bindingExangeB(@Qualifier("Bmessage")Queue cqueue, FanoutExchange exchange){
//        return BindingBuilder.bind(cqueue).to(exchange);
//    }//绑定

    @Bean
    Binding bindingExangeB2(@Qualifier("word")Queue cqueue, FanoutExchange exchange){
        return BindingBuilder.bind(cqueue).to(exchange);
    }//绑定



    @Bean
    Binding bindingExangeA(@Qualifier("Amessage")Queue cqueue, FanoutExchange exchange){
        return BindingBuilder.bind(cqueue).to(exchange);
    }//绑定

        //此处开始是死信队列代码
        //如果要配置死信队列，需要额外声明死信队列和死信交换机，绑定方法同上，但同时也要声明通过什么来接收正常队列接收不到的消息
        @Bean(name = "Amessage")
        public Queue Amessage(){
            Map<String, Object> args = new HashMap<>();
            //x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
            args.put("x-dead-letter-exchange", "deadLetterExchange");
            //x-dead-letter-routing-key  这里声明当前队列的死信路由key
            args.put("x-dead-letter-routing-key", "deadKey1");
            //此处使用路由键声明，消息未被正常接收就会跳到死信交换机再到死信队列
            args.put("x-message-ttl", 20000);
            return QueueBuilder.durable("Amessage").withArguments(args).build();
            //队列
        }
    //开始声明死信交换机
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange("deadLetterExchange");
    }
    //声明死信队列
    @Bean("deadLetterQueue1")
    public Queue deadLetterQueueA(){
        return new Queue("deadLetterQueue1");
    }
    //死信队列绑定死信交换机
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueue1") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("deadKey1");
    }//这里对应正常队列创建时的路由键
}
